import {messages} from "share/common";
import React from 'react';
import {
  Form,
  Card,
  Checkbox,
  Button,
  message
} from 'antd';

import airlineConfigService from './airline-config.service';

class AirlineConfigRefund extends React.Component {

  constructor() {
    super();
    this.state = {
      isSubmitting: false
    };
  }

  handleSubmit = (e) => {
    e.preventDefault();
    const { companyOID } = this.props;
    this.setState({ isSubmitting: true });
    this.props.form.validateFields((error, values) => {
      if (error) return;
      const data = {
        changeWay: values.changeWay
          .filter(v => ['0', '1', '2'].indexOf(v) > -1)
          .join(','),
        refundWay: values.refundWay
          .filter(v => ['0', '2', '4'].indexOf(v) > -1)
          .join(',')
      };

      airlineConfigService.updateCompanyConfig(companyOID)(data).then((res) => {
        if (res.data.code !== 200) return Promise.reject();
        return message.success(messages('common.operate.success')/* 操作成功 */);
      }).catch(() => {
        message.error(messages('common.operate.filed')/* 操作失败 */);
      }).finally(() => {
        this.setState({ isSubmitting: false });
      });
    });
  }

  componentDidMount = () => {
    const { companyOID } = this.props;
    airlineConfigService.getConfig(companyOID)('refundChange').then((res) => {
      if (res.data.code !== 200) return Promise.reject(res.data.message);
      const data = res.data.body;
      const refundWay = data.refundWay
        ? data.refundWay.split(',')
        : [];
      const changeWay = data.changeWay
        ? data.changeWay.split(',')
        : [];
      this.props.form.setFieldsValue({
        refundWay,
        changeWay
      });
    });
  }

  render = () => {
    const { getFieldDecorator } = this.props.form;
    const { isSubmitting } = this.state;
    return (
      <div className="airline-config-container">
        <Form onSubmit={this.handleSubmit}>
          <Card type="inner" title={messages('supplier.key574')/*改签方式*/}>
            <p className="hint">{messages('supplier.key575')/*提示：优行商旅暂不支持事前审批，若选择，会自动略过事前审批环节*/}</p>
            <Form.Item>
              {getFieldDecorator('changeWay')(
                <Checkbox.Group>
                  <Checkbox value={'0'}>{messages('supplier.key576')/*因公订单-无需审批*/}</Checkbox>
                  <Checkbox value={'1'}>{messages('supplier.key577')/*因公订单-事前审批*/}</Checkbox>
                  <Checkbox value={'2'}>{messages('supplier.key578')/*因公订单-事后审批*/}</Checkbox>
                </Checkbox.Group>
              )}
            </Form.Item>
          </Card>
          <Card type="inner" title={messages('supplier.key579')/*退票方式*/}>
            <p className="hint">{messages('supplier.key580')/*提示：目前只有傲翔商旅支持线上非自愿退票，其它供应商会走正常流程*/}</p>
            <Form.Item>
              {getFieldDecorator('refundWay')(
                <Checkbox.Group>
                  <Checkbox value={'0'}>{messages('supplier.key576')/*因公订单-无需审批*/}</Checkbox>
                  <Checkbox value={'2'}>{messages('supplier.key578')/*因公订单-事后审批*/}</Checkbox>
                  <Checkbox value={'4'}>{messages('supplier.key581')/*因公订单-非自愿退票*/}</Checkbox>
                </Checkbox.Group>
              )}
            </Form.Item>
          </Card>
          <Form.Item>
            <Button
              type="primary"
              htmlType="submit"
              loading={isSubmitting}
            >{messages('supplier.key250')/*保存配置*/}</Button>
          </Form.Item>
        </Form>
      </div>
    );
  }
}

const WrapppedAirlineRefund = Form.create()(AirlineConfigRefund)

export default WrapppedAirlineRefund;
